package com.szqhyxj.youxiangju.biz.receipt;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.format.Time;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.szqhyxj.youxiangju.BaseActivity;
import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.core.SharedPreferencesUtil;
import com.szqhyxj.youxiangju.biz.core.UserInfoManger;
import com.szqhyxj.youxiangju.biz.creditReport.demonstration.DemonstrationActivity;
import com.szqhyxj.youxiangju.biz.equipmentActivation.equipmentCardslot.EquipmentCardSlotActivity;
import com.szqhyxj.youxiangju.biz.equipmentActivation.headIdCard.HeadIdCardActivity;
import com.szqhyxj.youxiangju.biz.merchantHome.MerchantContainerActivity;
import com.szqhyxj.youxiangju.biz.merchantHome.deviceinfo.DeviceInfo;
import com.szqhyxj.youxiangju.biz.onlineChannel.OnlineChannelListActivity;
import com.szqhyxj.youxiangju.biz.receipt.receiptdetail.ReceiptDetailActivity;
import com.szqhyxj.youxiangju.biz.receipt.record.ReceiptRecordActicirty;
import com.szqhyxj.youxiangju.sys.components.NavigationBar;
import com.szqhyxj.youxiangju.sys.components.view.popview.ConsumePatternDialog;
import com.szqhyxj.youxiangju.sys.components.view.popview.OutlinePopView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by leung on 2018/11/23.
 */

public class ReceiptActivity extends BaseActivity implements ReceiptContract.View, NavigationBar.NavigationBarListener, ReceiptAdapter.IconClickListener, ConsumePatternDialog.ISharePlatformListener {

    @BindView(R.id.navigationBar)
    NavigationBar navigationBar;
    @BindView(R.id.receiptInfo)
    RecyclerView receiptInfo;
    ReceiptAdapter adapter;
    ReceiptPresenter presenter;
    ReceiptModel.ReceiptEntity receiptEntity;
    @BindView(R.id.none)
    LinearLayout none;
    @BindView(R.id.record)
    Button record;
    @BindView(R.id.receiptArea)
    LinearLayout receiptArea;
    @BindView(R.id.neticon)
    ImageView neticon;
    @BindView(R.id.nettitle)
    TextView nettitle;
    @BindView(R.id.netArea)
    LinearLayout netArea;
    @BindView(R.id.topbg)
    View topbg;
    @BindView(R.id.guider01)
    LinearLayout guider01;
    @BindView(R.id.guider02)
    LinearLayout guider02;
    @BindView(R.id.guider03)
    LinearLayout guider03;


    public static void start(Activity activity) {
        Intent intent = new Intent();
        intent.setClass(activity, ReceiptActivity.class);
        activity.startActivity(intent);

    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receipt);
        ButterKnife.bind(this);
        navigationBar.setNavigationBarListener(this);
        presenter = new ReceiptPresenter(this);
        presenter.init();
        presenter.setdeviceinfo();
        if (SharedPreferencesUtil.getReceiptShow()) {
            navigationBar.setVisibility(View.VISIBLE);
            topbg.setVisibility(View.VISIBLE);
            netArea.setVisibility(View.VISIBLE);
            receiptArea.setVisibility(View.VISIBLE);
            guider01.setVisibility(View.GONE);
            guider02.setVisibility(View.GONE);
            guider03.setVisibility(View.GONE);
        } else {
            navigationBar.setVisibility(View.GONE);
            topbg.setVisibility(View.GONE);
            netArea.setVisibility(View.GONE);
            receiptArea.setVisibility(View.GONE);
            guider01.setVisibility(View.VISIBLE);
            guider02.setVisibility(View.GONE);
            guider03.setVisibility(View.GONE);
        }

    }


    @Override
    public void navigationLeft() {
        MerchantContainerActivity.start(this, true);
    }

    @Override
    public void navigationRight() {

    }

    @Override
    public void showList(List<ReceiptModel.ReceiptEntity> list) {
        if (list.size() == 0) {
            none.setVisibility(View.VISIBLE);
            receiptArea.setVisibility(View.GONE);
        } else {
            none.setVisibility(View.GONE);
            receiptArea.setVisibility(View.VISIBLE);
            receiptInfo.setOverScrollMode(View.OVER_SCROLL_NEVER);
            receiptInfo.setLayoutManager(new GridLayoutManager(this, 1));
            adapter = new ReceiptAdapter(list, this);
            receiptInfo.setAdapter(adapter);
        }

    }

    @Override
    public void showuserinfo() {
        if (UserInfoManger.INSTANCE.getUser().getActiviteStatus() == 0) {
            netArea.setVisibility(View.GONE);
        } else {
            netArea.setVisibility(View.VISIBLE);
            if (DeviceInfo.Holder.getInstance().getOnlineState().equals("1")) {
                neticon.setImageDrawable(getResources().getDrawable(R.mipmap.poswangluo));
                nettitle.setText(DeviceInfo.Holder.getInstance().getAepnType());
                nettitle.setTextColor(getResources().getColor(R.color.white));
            } else {
                neticon.setImageDrawable(getResources().getDrawable(R.mipmap.poswangluo1));
                nettitle.setText("离线");
                nettitle.setTextColor(getResources().getColor(R.color.texttm));
            }


        }
    }

    @Override
    public void enshowuserInfo() {
        netArea.setVisibility(View.GONE);
    }


    @Override
    public void onClickItem(int i, List<ReceiptModel.ReceiptEntity> receiptEntities) {
        receiptEntity = receiptEntities.get(i);
        ConsumePatternDialog consumePatternDialog = new ConsumePatternDialog(this);

        consumePatternDialog.setTitleDevice("设备收款");
        consumePatternDialog.setTitleOnline("快捷收款");
        consumePatternDialog.setnavigationbarTitle("收款方式");
        //设备是否激活 激活--是否在线   在线---是否报备   未在线---提示上线   未报备---去报备   报备---进行交易   未激活---去激活
        if (UserInfoManger.INSTANCE.getUser().getActiviteStatus() == 0) {
            consumePatternDialog.setEquipmentContent("设备未激活，点击进行设备激活");
            consumePatternDialog.setEquipmentContentColor(getResources().getColor(R.color.tipcolor));
        } else {
            //已激活
            if (DeviceInfo.Holder.getInstance().getOnlineState().equals("1")) {
                //在线
                //判断是否报备
                if (receiptEntity.getBankStates() == 0) {
                    //未报备
                    consumePatternDialog.setEquipmentContent("信用卡未报备，点击进行实名报备");
                    consumePatternDialog.setEquipmentContentColor(getResources().getColor(R.color.tipcolor));
                } else {
                    //已报备
                    consumePatternDialog.setEquipmentContent("费率：0.79%+单笔1.0元");
                    consumePatternDialog.setEquipmentContentColor(getResources().getColor(R.color.white));
                }
            } else {
                //未在线
                consumePatternDialog.setEquipmentContent("您的设备已离线，无法正常进行交易");
                consumePatternDialog.setEquipmentContentColor(getResources().getColor(R.color.tipcolor));
            }
        }

/*
        if (DeviceInfo.Holder.getInstance().getOnlineState().equals("1")) {
            if (receiptEntity.getBankStates() == 0) {
                if (UserInfoManger.INSTANCE.getUser().getActiviteStatus() == 0) {
                    consumePatternDialog.setEquipmentContent("设备未激活，点击进行设备激活");
                    consumePatternDialog.setEquipmentContentColor(getResources().getColor(R.color.tipcolor));

                } else {
                    consumePatternDialog.setEquipmentContent("信用卡未报备，点击进行实名报备");
                    consumePatternDialog.setEquipmentContentColor(getResources().getColor(R.color.tipcolor));

                }
            } else {
                consumePatternDialog.setEquipmentContent("费率：0.85%+单笔0.5元");
                consumePatternDialog.setEquipmentContentColor(getResources().getColor(R.color.white));

            }
        } else {
            consumePatternDialog.setEquipmentContent("您的设备已离线，无法正常进行交易");
            consumePatternDialog.setEquipmentContentColor(getResources().getColor(R.color.tipcolor));

        }*/
        consumePatternDialog.setOnClickListener(new ConsumePatternDialog.ISharePlatformListener() {
            @Override
            public void onEquipmentRefund(View view) {
                consumePatternDialog.dismiss();
                if (receiptEntity.getBankStates() == 0) {
                    if (UserInfoManger.INSTANCE.getUser().getActiviteStatus() == 0) {

                        HeadIdCardActivity.start(ReceiptActivity.this);
                        consumePatternDialog.dismiss();
                    } else {
                        DemonstrationActivity.start(ReceiptActivity.this);
                        consumePatternDialog.dismiss();
                    }
                } else {
                    if (DeviceInfo.Holder.getInstance().getOnlineState().equals("1")) {

                        if (isCurrentInTimeScope(10, 30, 19, 50)) {
                            ReceiptDetailActivity.start(ReceiptActivity.this, receiptEntities.get(i).getBtName(), receiptEntities.get(i).getBankCardNo(), receiptEntity.getFixedAmount(), receiptEntity.getCardPhone(),"","","1","100","30000",0,0);
                            consumePatternDialog.dismiss();
                        } else {
                            Toast.makeText(ReceiptActivity.this, "温馨提示：不在收款时间范围！请在10：30-19：50时间范围内进行！", Toast.LENGTH_LONG).show();
                        }
                    } else {
                        consumePatternDialog.dismiss();
                        OutlinePopView outlinePopView = new OutlinePopView(ReceiptActivity.this);
                        outlinePopView.setOnClickListener(new OutlinePopView.OnClickListener() {
                            @Override
                            public void onClickConfirm(View v) {
                                EquipmentCardSlotActivity.start(ReceiptActivity.this);
                            }
                        });
                        outlinePopView.show();
                    }
                }
            }

            @Override
            public void onShortcutRefund(View view) {
                consumePatternDialog.dismiss();
                OnlineChannelListActivity.start(ReceiptActivity.this, receiptEntities.get(i).getCardId(), receiptEntities.get(i).getBtName(), receiptEntities.get(i).getBankCardNo(), receiptEntity.getFixedAmount(), "2", receiptEntities.get(i).getCardPhone(),receiptEntities.get(i).getBillingDate(),receiptEntities.get(i).getDueDate(),receiptEntities.get(i).getAreainfo());
                consumePatternDialog.dismiss();

            }
        });

        consumePatternDialog.show();

    }

            /* else{
                if (DeviceInfo.Holder.getInstance().getOnlineState().equals("1")) {
                    if (isCurrentInTimeScope(10, 30, 19, 50)) {
                        ReceiptDetailActivity.start(this, receiptEntity.getBtName(), receiptEntity.getBankCardNo(), receiptEntity.getFixedAmount(), receiptEntity.getCardPhone());
                    } else {
                        Toast.makeText(this, "温馨提示：不在收款时间范围！请在10：30-19：50时间范围内进行！", Toast.LENGTH_LONG).show();
                    }

                } else {
                    OutlinePopView outlinePopView = new OutlinePopView(this);
                    outlinePopView.setOnClickListener(new OutlinePopView.OnClickListener() {
                        @Override
                        public void onClickConfirm(View v) {
                            EquipmentCardSlotActivity.start(ReceiptActivity.this);
                        }
                    });
                    outlinePopView.show();
                }
                 */


    public static boolean isCurrentInTimeScope(int beginHour, int beginMin, int endHour, int endMin) {
        boolean result = false;
        final long aDayInMillis = 1000 * 60 * 60 * 24;
        final long currentTimeMillis = System.currentTimeMillis();
        Time now = new Time();
        now.set(currentTimeMillis);
        Time startTime = new Time();
        startTime.set(currentTimeMillis);
        startTime.hour = beginHour;
        startTime.minute = beginMin;
        Time endTime = new Time();
        endTime.set(currentTimeMillis);
        endTime.hour = endHour;
        endTime.minute = endMin;
        if (!startTime.before(endTime)) {
            startTime.set(startTime.toMillis(true) - aDayInMillis);
            result = !now.before(startTime) && !now.after(endTime); // startTime <= now <= endTime
            Time startTimeInThisDay = new Time();
            startTimeInThisDay.set(startTime.toMillis(true) + aDayInMillis);
            if (!now.before(startTimeInThisDay)) {
                result = true;
            }
        } else {
            result = !now.before(startTime) && !now.after(endTime); // startTime <= now <= endTime
        }
        return result;
    }

    @Override
    public void onEquipmentRefund(View view) {
        /*if (receiptEntity.getBankStates() == 0) {     //信用卡是否报备  0未报备  1计划执行中  2制定计划
            if (UserInfoManger.INSTANCE.getUser().getActiviteStatus() == 0) {   //设备是否激活  0未激活 1设备已激活
                HeadIdCardActivity.start(this);

            } else {
                DemonstrationActivity.start(this);
            }
        }

        if (receiptEntity.getBankStates() == 1) {

        }

        if (receiptEntity.getBankStates() == 2) {

                ReceiptDetailActivity.start(this, receiptEntity.getBtName(), receiptEntity.getBankCardNo(), receiptEntity.getFixedAmount());

        }*/
    }

    @Override
    public void onShortcutRefund(View view) {
        //  OnlineChannelListActivity.start(getActivity(),cardNo);
    }


    @OnClick({R.id.netArea, R.id.record, R.id.next01, R.id.next02, R.id.know01})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.netArea:
                EquipmentCardSlotActivity.start(this);
                break;
            case R.id.record:
                ReceiptRecordActicirty.start(this,"device");
                break;
            case R.id.next01:
                guider01.setVisibility(View.GONE);
                guider02.setVisibility(View.VISIBLE);
                guider03.setVisibility(View.GONE);
                break;
            case R.id.next02:
                guider01.setVisibility(View.GONE);
                guider02.setVisibility(View.GONE);
                guider03.setVisibility(View.VISIBLE);
                break;
            case R.id.know01:
                navigationBar.setVisibility(View.VISIBLE);
                topbg.setVisibility(View.VISIBLE);
                netArea.setVisibility(View.VISIBLE);
                receiptArea.setVisibility(View.VISIBLE);
                guider01.setVisibility(View.GONE);
                guider02.setVisibility(View.GONE);
                guider03.setVisibility(View.GONE);
                SharedPreferencesUtil.setReceiptShow(true);
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            MerchantContainerActivity.start(this, true);
        }
        return super.onKeyDown(keyCode, event);
    }


}
